package com.atkxp.stustatusmasystem.controller;

import com.atkxp.stustatusmasystem.pojo.Class;
import com.atkxp.stustatusmasystem.pojo.Major;
import com.atkxp.stustatusmasystem.pojo.Teacher;
import com.atkxp.stustatusmasystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author user
 * @declaration
 * @create 2023/6/4-14:59
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    public StudentService studentService;

    @Autowired
    public TeacherService teacherService;

    @Autowired
    public MajorService majorService;

    @Autowired
    public ClassService classService;

    @Autowired
    public ApplicationService applicationService;

    @ResponseBody
    @RequestMapping("hello")
    public String sayHello() {
        return "hello";
    }

    @RequestMapping("toAddTeacher")
    public String toAddTeacher(Model model) {
        List<Major> majors = majorService.getAllMajor();
        model.addAttribute("majors", majors);
        return "addTeacher";
    }

    @RequestMapping("addTeacher")
    @ResponseBody
    public int addTeacher(int id, String tid, String name, String clazz, String password, int sex, int majorid, int right) {
        teacherService.addTeacher(new Teacher(id, name, password, tid, sex, majorid, right));
        return classService.addClass(new Class(0,clazz, tid));
    }

    @RequestMapping("toUpdateTeacher")
    public String toUpdateTeacher(int id, Model model) {
        Teacher teacher = teacherService.getTeacherById(id);
        model.addAttribute("teacher", teacher);
        return "updateTeacher";
    }

    @RequestMapping("updateTeacher")
    @ResponseBody
    public int updateTeacher(int id, String tid, String name, String password, int sex, int right) {
        Teacher teacher = teacherService.getTeacherByTid(tid);
        return teacherService.updateTeacherById(new Teacher(id, name, password, tid, sex, teacher.getMajorid(), right));
    }

    @RequestMapping("deleteTeacher")
    @ResponseBody
    public int deleteTeacher(int id) {
        return teacherService.deleteTeacherById(id);
    }

    @RequestMapping("getAllTeacher")
    public String getAllTeacher(Model model) {
        List<Teacher> teachers = teacherService.getAllTeacher();
        Long count = teacherService.getTeacherCount();
        model.addAttribute("teachers", teachers);
        model.addAttribute("teacherCount", count);
        return "showAllTeacher";
    }

}
